/**
 * @param {number[]} nums
 * @return {number[]}
 */
 var nextGreaterElements = function(nums) {
    let res = new Array(nums.length).fill(-1)
    let stack = []
    stack.push(0)
    for(let i = 1;i < 2 * nums.length;i++){
      while(stack.length && nums[i % nums.length] > nums[stack[stack.length-1]]){
        let index = stack.pop()
        res[index] = nums[i % nums.length]
      }
      stack.push(i % nums.length)
    }
    return res
};